<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\QuickDemand;
use App\Models\Areas;
use App\Models\BidLists;
use App\Models\Buys;
use App\Models\Categorys;
use App\Models\CompanyInfos;
use App\Models\QuickDemands;
use App\Models\Mails;
use App\Models\Messages;
use App\Models\Stores;
use App\Models\Users;
use Illuminate\Http\Request;

class QuickDemandsController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(Request $request){
        $params = [
            'page' => 1,
            'kwd' => '',
            'status' => 0,
            'start' => '',
            'end' => '',
        ];
        //如果从代办点击过来，则显示待审核
        $from = $request->get('from',0);
        if ($from == 1){
            $params['status'] = 1;
            $from = 0;
        }
        $params = json_encode($params);
        return view('admin.demand.quick_index',compact('params'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'status' => (int)$request->get('status',0),
                'type' => (int)$request->get('type',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
                'sort' => (string)$request->get('sort',''),
            ];
            $lists = QuickDemands::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 对接
     * @param Request $request
     * @return bool
     */
    public function butt(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = QuickDemands::findOrFail($id);
            $info->update(['status' => QuickDemands::STATUS_2]);
            //发送站内信
            if($info['user_id'] != null){
                $template = [
                    'title' => '需求对接通知',
                    'content' => '您快速发布的需求平台已对接!',
                    'user_id' => $info['user_id'],
                    'status' => 1,
                    'type' => 1,
                ];
                Mails::create($template);
            }
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 发布
     */
    public function butt1(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = QuickDemands::findOrFail($id);
            $info->update(['type' => QuickDemands::TYPE_2]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            QuickDemands::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }




}
